//
//  NAStyledTableViewCell.m
//  NewApp
//
//  Created by  on 12-7-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NAStyledTableViewCell.h"
#import "Constants.h"
#import "AppDelegate.h"

@implementation NAStyledTableViewCell

@synthesize imageURL,name,createAt,info,isChild,sID,isExpanded,backgroundImage,showsAccessory;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id) initWithNAStyle:(NSString *) style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
        
        if (style == nil  || style.length == 0) {
            currentStyle = NAStyle_B;
        }
        
        if ([style isEqualToString:kStyleB]) {
            currentStyle = NAStyle_B;
        }
        
        if ([style isEqualToString:kStyleC]) {
            currentStyle = NAStyle_C;
        }
        
        if ([style isEqualToString:kStyleD]) {
            currentStyle = NAStyle_D;
        }
        
        self.isChild = NO;
    }
    
    return self;
}

- (void) dealloc {
    [imageURL release];
    [name release];
    [createAt release];
    [info release];
    [sID release];
    [backgroundImage release];
    [super dealloc];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [self setHighlighted:selected];
    [super setSelected:selected animated:animated];
}

//- (void) drawRect:(CGRect)rect {
//}

- (void) update {
//    currentMessage = [message retain];
    
    [self setNeedsDisplay];
}

- (void) drawContentView:(CGRect)rect highlighted:(BOOL)highlighted {
    CGContextRef c = UIGraphicsGetCurrentContext();
	
    if (self.backgroundImage != nil) {
        [self.backgroundImage drawInRect:rect];
    }
    
	// draw background with a gradient colour when it's selected
	if (self.selected) {
		CGColorSpaceRef colourspace = CGColorSpaceCreateDeviceRGB();
		static CGFloat gradLoc[] = {0.0, 1.0};
		static CGFloat gradLocColor[] = {
			0.79685, 0.79685, 0.79685, 1.0,
			0.66, 0.66, 0.66, 1.0
		};
		CGGradientRef grad = CGGradientCreateWithColorComponents(colourspace,
																 gradLocColor,
																 gradLoc, 2);
		CGColorSpaceRelease(colourspace);
		CGContextDrawLinearGradient(c, grad, CGPointMake(0, 0),
									CGPointMake(0, self.frame.size.height - 1), 0);
		CGGradientRelease(grad);
	}    
    CGContextRef context = UIGraphicsGetCurrentContext();
    //Set the stroke (pen) color
    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
    //Set the width of the pen mark
    CGContextSetLineWidth(context, 1.0);
    
    // Draw a line
    //Start at this point
    CGContextMoveToPoint(context, 0.0, 0.0);
    
    //Give instructions to the CGContext
    //(move "pen" around the screen)
    CGContextAddLineToPoint(context, 320.0, 0.0);
    
    
    //Draw it
    CGContextStrokePath(context);
    
    
    CGFloat margin = 0.f;
    
    if (isChild) {
        margin += 10.f;
    }
    
    //Draw image
    if (NAStyle_D != currentStyle) {
        if (self.imageURL != nil) {
            CGRect imageRect = (currentStyle == NAStyle_B) ? CGRectMake(5.f + margin, rect.size.height / 2 - 35.f, 90.f, 70.f) : CGRectMake(210.f + margin, 5.f, 100.f, 80.f);
            UIImage *image = [g_imageLoader cachedImageForURL:[NSURL URLWithString:self.imageURL] target:self];
            if (image != nil) {
                [image drawInRect:imageRect];
            }
        }
    }
    
    //Draw title
    if (self.name != nil) {
        CGRect titleRect = (NAStyle_B == currentStyle) ? CGRectMake(105.f + margin, rect.size.height / 2 - 35.f, 180.f, 20.f) : CGRectMake(5.f + margin, 5.f, 180.f, 20.f);
        [[UIColor whiteColor] set];
        [self.name drawInRect:titleRect withFont:[UIFont boldSystemFontOfSize:18.f] lineBreakMode:UILineBreakModeTailTruncation];
        
        
    }
    
    
    //Draw date
    if (self.createAt != nil) {
        CGRect dateRect = (NAStyle_B == currentStyle) ? CGRectMake(105.f + margin, 30.f, 180.f, 20.f) : CGRectMake(5.f + margin, 30.f, 180.f, 20.f);
        [self.createAt drawInRect:dateRect withFont:[UIFont systemFontOfSize:14.f] lineBreakMode:UILineBreakModeTailTruncation];
    }
    
    if (NAStyle_D != currentStyle) {
        CGRect infoRect = (NAStyle_B == currentStyle) ? CGRectMake(105.f + margin, 55.f, 180.f, 20.f) : CGRectMake(5.f + margin,55.f, 180.f, 20.f);
        if (self.info != nil) {
            [self.info drawInRect:infoRect withFont:[UIFont systemFontOfSize:14.f] lineBreakMode:UILineBreakModeTailTruncation];
        }
    }
    
    if ((!self.isChild) && (self.showsAccessory)) {
        
        if (self.isExpanded) {
            [[UIImage imageNamed:@"expand_icon.png"] drawInRect:CGRectMake(290.f, self.frame.size.height / 2 - 4.f, 16.f, 9.f)];
        }else {
            [[UIImage imageNamed:@"next_icon.png"] drawInRect:CGRectMake(290.f, self.frame.size.height / 2 - 8.f, 9.f, 16.f)];
        }
        
    }    

}

#pragma mark -
#pragma mark - Imageloading delegate

- (void)didLoadImage:(NSURL *)imageURL Image:(UIImage *)image {
    [self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
}



@end
